package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.zuipin.entity.SysUser;
import com.zuipin.entity.SysUserRole;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.SysUserMapper;
import com.zuipin.mapper.SysUserRoleMapper;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.Convert;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Encryption;
import com.zuipin.util.Pagination;
import com.zuipin.util.StringUtil;

import net.sf.json.JSONObject;

@Service
public class SysUserServiceImpl implements ISysUserService {
	@Resource
	private SysUserMapper		sysUserMapper;
	
	@Resource
	private SysUserRoleMapper	sysUserRoleMapper;
	
	@Override
	public SysUser findById(Long id) {
		return sysUserMapper.findById(id);
	}
	
	@Override
	public void insert(SysUser sysUser) {
		sysUserMapper.insert(sysUser);
	}
	
	@Override
	public void update(SysUser sysUser) {
		sysUserMapper.update(sysUser);
	}
	
	@Override
	public SysUser login(JSONObject obj) {
		// TODO Auto-generated method stub
		return sysUserMapper.login(obj);
	}
	
	@Override
	public SysUser findByAccount(String account) {
		// TODO Auto-generated method stub
		return sysUserMapper.findByAccount(account);
	}
	
	/**
	 * 
	 * @Title: findByIds 
	 * @Description: 根据多个Id查询系统用户
	 * @author xujinding
	 * @param @param ids
	 * @param @return
	 * @throws
	 */
	@Override
	public List<SysUser> findByIds(List<Long> ids) {
		return sysUserMapper.findByIds(ids);
	}
	
	@Override
	public List<SysUser> findBySysUserSelected(SysUser sysUser, Pagination page) {
		// TODO Auto-generated method stub
		return sysUserMapper.findBySysUserSelected(sysUser, page);
	}
	
	@Override
	public Result<SysUser> saveUser(SysUser sysUser, String roleIds) {
		Result<SysUser> result = new Result<>();
		
		String operTime = DateUtils.getCurrentDateTime();
		Long userId = sysUser.getId();
		try {
			if (userId == null) {
				sysUser.setCreateTime(operTime);
				sysUser.setUpdateTime(operTime);
				sysUser.setCreateId(sysUser.getUpdateId());
				sysUser.setDelFlag(false);
				sysUser.setStatus(SysUser.ENABLE);
				sysUser.setPassword(Encryption.md5(SysUser.DEFAULT_PASSWORD));
				insert(sysUser);
				userId = sysUser.getId();
				result.setMessage("新增用户成功");
				result.setCnMessage("新增用户成功");
			} else {
				sysUser.setUpdateTime(operTime);
				update(sysUser);
				result.setMessage("更新用户信息成功");
				result.setCnMessage("更新用户信息成功");
			}
		} catch (Exception e) {
			// 违反唯一约束
			if (e.getMessage().indexOf(SysUser.UQ_ACCOUNT_INDEX_KEY) >= 0) {
				result.setErrorCode(ErrorCode.OBJECT_EXIST);
				result.setMessage("用户帐号已经存在,请重新输入");
				result.setCnMessage("用户帐号已经存在,请重新输入");
				result.setData(null);
				return result;
			} else {
				result.setErrorCode(ErrorCode.DB_EXCEPTION);
				result.setData(null);
				return result;
			}
		}
		// 更新角色用户关系
		if (StringUtil.isNotBlank(roleIds)) {
			String[] roleIdArr = roleIds.split(",");
			// 删除角色用户关系
			sysUserMapper.deleteUserRoleRelationship(userId);
			// 新增角色用户关系
			for (String roleId : roleIdArr) {
				if (StringUtils.isBlank(roleId)) {
					continue;
				}
				roleId = roleId.replace(" ", "");
				if (StringUtils.isNumeric(roleId)) {
					// 保存用户角色关系
					sysUserRoleMapper.insert(new SysUserRole(userId, Convert.toLong(roleId)));
				} else {
					result.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					result.setMessage("roleIds格式错误！");
					result.setCnMessage("roleIds格式错误！");
					return result;
				}
			}
		}
		
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@Override
	public Result<SysUser> updatePassword(Long userId, String oldPassWord, String newPassWord, Long updateId) {
		Result<SysUser> result = new Result<>();
		
		if (newPassWord.equals(oldPassWord)) {
			result.setErrorCode(ErrorCode.OLD_IS_NOT_EQUALS_NEW);
			return result;
		}
		// 验证密码
		SysUser user = findById(userId);
		if (user == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setCnMessage("用户不存在！");
			result.setCnMessage("用户不存在！");
			return result;
		}
		if (!Encryption.md5(oldPassWord).equals(user.getPassword())) {
			result.setErrorCode(ErrorCode.PASSWORD_ERROR);
			result.setCnMessage("旧密码输入错误！");
			result.setCnMessage("旧密码输入错误！");
			return result;
		}
		user.setPassword(Encryption.md5(newPassWord));
		user.setUpdateId(updateId);
		user.setUpdateTime(DateUtils.getCurrentDateTime());
		sysUserMapper.update(user);
		
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setCnMessage("密码修改成功！");
		result.setMessage("密码修改成功！");
		return result;
	}
	
	@Override
	public Result<SysUser> resetPassWord(Long userId, String newPassWord, Long updateId) {
		Result<SysUser> result = new Result<>();
		SysUser user = sysUserMapper.findById(userId);
		if (user == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setCnMessage("用户不存在！");
			result.setCnMessage("用户不存在！");
			return result;
		}
		user.setPassword(Encryption.md5(newPassWord));
		user.setUpdateId(updateId);
		user.setUpdateTime(DateUtils.getCurrentDateTime());
		sysUserMapper.update(user);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setCnMessage("密码重置成功！");
		result.setMessage("密码重置成功！");
		return result;
	}
	
	@Override
	public Result<SysUser> checkUserPassWord(Long userId, String passWord) {
		Result<SysUser> result = new Result<>();
		SysUser user = sysUserMapper.findById(userId);
		if (user == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setCnMessage("用户不存在！");
			result.setCnMessage("用户不存在！");
			return result;
		}
		if (user.getPassword().equals(Encryption.md5(passWord))) {
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.PASSWORD_ERROR);
			result.setCnMessage("密码错误！");
			result.setCnMessage("密码错误！");
		}
		return result;
	}
	
}
